#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/wait.h>

int main()
{
    pid_t id = fork();
    if(id == 0)
    {// child
        int cnt = 5;
        while(cnt--)
        {
            printf("我是子进程: pid: %d\n", getpid());
            sleep(10);
        }
        exit(0);// 终止子进程
    }
    else if(id > 0)
    {// parent
       // while(1)
       // {
       //     printf("我是父进程: pid: %d\n", getpid());
       //     sleep(1);
       // }
       // 回收子进程，等待僵尸
       //pid_t rid = wait(NULL);
       int status = 0;
       pid_t rid = waitpid(id, &status, 0);
       if(rid == id)
       {
           int exit_code = ((status >> 8)&0xFF);
           int exit_sig =  status&0x7F; // 0111 1111
           printf("pid: %d, wait success!, status: %d, exit_code: %d, exit_sig: %d\n",\
                   getpid(), status, exit_code, exit_sig);
       }
    }
}
